package org.rufolfheszele.andpixelbyandpixel;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import android.graphics.Bitmap;
import android.graphics.Point;

/**
 * This class handles the events in the game initial state
 *
 * The initial state is set when the game is just started, or when the user closes an already started level
 * @see IGameStateController
 * @author Rudolf Heszele heszele@gmail.com
 * @version 1.0
 * @since 2012-03-08
 */
public class IdleGameStateController implements IGameStateController
{
	/**
	 * The default constructor can optionally take an argument
	 *
	 * The optionally argument is taken only when the application is started, because in this case the MainPanel can not be reached through
	 * the wxGetApp method, since the window itself is not constructed yet by the wxWidgets
	 * @param mainPanel The pointer to the MainPanel placed on the window
	 */
	public IdleGameStateController(MainImageView mainImageView)
	{
		Socket clientSocket = MainApplication.getMainApplication().getClientSocket();
		ServerSocket serverSocket = MainApplication.getMainApplication().getServerSocket();

		mLogoDrawController = new LogoDrawController(mainImageView, true);
		// Stop both the server- and the client socket if needed
		if(null != clientSocket)
		{
			try
			{
				clientSocket.close();
			}
			catch(IOException exception)
			{
				// Nothing to do
			}
			MainApplication.getMainApplication().setClientSocket(null);
		}
		if(null != serverSocket)
		{
			// TODO: implement this
			// When destroying the server, we need to deregister it first
			/*ServerRegistrationHandler::ServerInfo serverInfo;
			wxString response;

			serverInfo.setPort(wxGetApp().getConfiguration()->getNetworkConfiguration()->getPortNumber());
			if(false == ServerRegistrationHandler::deregisterServer(serverInfo, response))
				wxGetApp().getMainFrame()->showMessageDialog(wxT("Could not update the server's state: " + response), MainFrame::SERVER_ERROR);*/
			try
			{
				serverSocket.close();
			}
			catch(IOException exception)
			{
				// Nothing to do
			}
			MainApplication.getMainApplication().setServerSocket(null);
		}
		MainApplication.getMainApplication().resetActualLevel();
		if(null == mainImageView)
		{
			// Enable or disable menu points according to the idle state
			/*wxGetApp().getMainFrame()->setHostEnabled(true);
			wxGetApp().getMainFrame()->setJoinEnabled(true);
			wxGetApp().getMainFrame()->setRestartEnabled(false);
			wxGetApp().getMainFrame()->setMessagingEnabled(false);
			wxGetApp().getMainFrame()->setConfigureEnabled(true);
			wxGetApp().getMainFrame()->GetStatusBar()->SetStatusText(wxT("Idle"));
			wxGetApp().getMainFrame()->setStartHostingEnabled(true);
			wxGetApp().getMainFrame()->setChangeLevelEnabled(false);
			wxGetApp().getMainFrame()->setKickOffClientEnabled(false);*/
		}
	}


	// Methods inherited from Controller

	/**
	 * @see IController::handleMouseDown
	 */
	public boolean handleTouchDown(Point point)
	{
		// Nothing to do
		return false;
	}
	
	/**
	 * @see IController::handleMouseUp
	 */
	public boolean handleTouchUp(Point point)
	{
		// Nothing to do
		return false;
	}
	
	/**
	 * @see IController::handleMouseMotion
	 */
	public boolean handleTouchMove(Point point)
	{
		// Nothing to do
		return false;
	}
	
	/**
	 * @see IController::getBitmapToPaint
	 */
	public Bitmap getBitmap()
	{
		return mLogoDrawController.getBitmap();
	}

	// Methods inherited from IGameStateController

	/**
	 * @see IGameStateController::handleSocketConnection
	 */
	public void handleSocketConnection(Socket socket)
	{
		// Nothing to do
	}
	
	/**
	 * @see IGameStateController::handleSocketLost
	 */
	public void handleSocketLost(Socket socket)
	{
		// Nothing to do
	}
	
	/**
	 * @see IGameStateController::handleSocketInput
	 */
	public void handleSocketInput(Socket socket)
	{
		// Nothing to do
	}
	
	/**
	 * @see IGameStateController::handleSocketOutput
	 */
	public void handleSocketOutput(Socket socket)
	{
		// Nothing to do
	}

	/**
	 * This object handles the drawings during this state of the application
	 *
	 * @see LogoDrawController
	 */
	private LogoDrawController mLogoDrawController = null;
}
